import re
import requests
import DBUtil
from lxml import etree
import Sha1Util
from concurrent.futures import ThreadPoolExecutor
import time

headers = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36"
}


def create_job():
    """
    创建表
    :return:
    """
    sql = """
    create table if not exists jobDetailed (
    job_id varchar(64) not null primary key comment "招聘id", 
    job_title varchar(100) not null comment "招聘标题",
    job_url varchar(64) not null comment "招聘链接", 
    work_type varchar(32) not null comment "职业类型",
    job_number int(10) not null comment "招聘人数",
    job_place varchar(100) comment "上班地点",
    time_resp varchar(64) comment "时间要求",
    job_type varchar(100) comment "工作种类",
    least_weekly varchar(64) comment "每周至少",
    job_time varchar(100) comment "工作时间",
    work_time varchar(64) comment "上班时段",
    how_pay varchar(64) comment "结算方式",
    job_price varchar(100) comment "基本工资",
    job_detail varchar(4096) comment "工作详情",
    c_name varchar(128) comment "公司名称",
    c_info varchar(4096) comment "公司介绍",
    c_address varchar(128) comment "公司地址"
    );
    """
    results = DBUtil.createDatabase(sql)
    if results == 500:
        print("创建失败")
    else:
        print("创建成功")


def getJobUrl():
    sql = 'select url,titleName from job1'
    results = DBUtil.queryData(sql)
    return results


# @retry(stop_max_attempt_number=3, wait_random_min=1000, wait_random_max=5000)
def getJobDetail():
    """
    获取兼职信息
    :return:
    """
    result = getJobUrl()
    if result != 500:
        for item in result:
            # 招聘url
            job_url = item[0]
            # 招聘标题
            job_title = item[1]
            # 招聘id
            job_id = Sha1Util.jia_mi(job_title + job_url)
            resp = requests.get(url=job_url, headers=headers)
            try:
                if resp.status_code == 200:

                    resp.encoding = "utf-8"
                    page_text = resp.text
                    # 职业类型
                    work_type = re.findall('<a class="job_type"  href=".*?"  target="blank">(.*?)</a>', page_text, re.S)[0]
                    # 招聘人数
                    job_number = re.findall('<li><span class="tit">招聘人数: </span><span class="con">(.*?)</span></li>', page_text, re.S)[0]
                    # 上班地点
                    job_place = re.findall('<li><span class="tit">上班地点: </span><span class="con">(.*?)</span></li>', page_text, re.S)[0]
                    # 工作种类
                    job_type = re.findall('<li><span class="tit">工作种类: </span><span class="con">(.*?)</span></li>', page_text, re.S)[0]
                    # 时间要求
                    time_resp = re.findall('<li><span class="tit">时间要求: </span><span class="con">(.*?)</span></li>', page_text, re.S)
                    if len(time_resp) > 0:
                        time_resp = time_resp[0]
                    else:
                        time_resp = ""
                    # 每周至少
                    least_weekly = re.findall('<li><span class="tit">每周至少: </span><span class="con">(.*?)</span></li>', page_text, re.S)
                    if len(least_weekly) > 0:
                        least_weekly = least_weekly[0]
                    else:
                        least_weekly = ""
                    # 工作时间
                    job_time = re.findall('<li><span class="tit">工作时间: </span><span class="con">(.*?)</br></span></li>', page_text, re.S)
                    if len(job_time) > 0:
                        job_time = job_time[0]
                    else:
                        job_time = ""
                    # 上班时段
                    work_time = re.findall('<li><span class="tit">上班时段: </span><span class="con">(.*?)</span></li>', page_text, re.S)[0]
                    # 结算方式
                    how_pay = re.findall('<li><span class="tit">结算方式: </span><span class="con">(.*?)</span></li>', page_text, re.S)[0]
                    # 基本工资
                    job_price = re.findall('<li><span class="tit">基本工资: </span><span class="con">(.*?)</span></li>', page_text, re.S)[0]

                    # 时间要求 每周至少
                    # 工作时间

                    tree = etree.HTML(page_text)
                    job_detail_list = tree.xpath('/html/body/section/article/div/div[1]/div[2]/div[2]/div[1]/div/text()')
                    # 工作详情
                    job_detail = ",".join(job_detail_list)  # 根据实际需要使用相应的分隔符连接列表元素,如 , : ; 或者空字符串
                    # 公司名称
                    c_name = re.findall('target="_blank" class="com_name">(.*?)</a>', page_text, re.S)[0]
                    # 公司介绍
                    c_info = re.findall('<p><span class="label">公司介绍：</span>(.*?)</p>', page_text, re.S)[0]
                    # 公司地址
                    c_address = re.findall('<p><span class="label">公司地址：</span>(.*?)</p>', page_text, re.S)[0]

                    # 创建表
                    sql = f"""
                    insert ignore into jobDetailed values (
                    '{job_id}','{job_title}','{job_url}','{work_type}','{job_number}',
                    '{job_price}','{time_resp}','{job_type}','{least_weekly}','{job_time}',
                    '{work_time}','{how_pay}','{job_price}','{job_detail}','{c_name}','{c_info}','{c_address}');
                    """
                    results = DBUtil.modifyData(sql)
                    if results == 200:
                        print("写入成功")
                    else:
                        print("写入失败")
                    time.sleep(0.2)

                else:
                    pass

            except Exception as e:
                print("获取区域信息失败%s" % e)
    else:
        print("查询失败")


create_job()
with ThreadPoolExecutor(50) as th:
    th.submit(getJobDetail)
getJobDetail()

